


import $ from 'zepto-webpack'
import Swiper from 'swiper';  
import myReward from './myReward.js'
import rewardData from './rewardData'
import Toast from './Toast'
import '../css/reset.css'
import '../css/main.scss'  


const currentDrawNumMap ={
    0:"zero",
    1:"one",
    2:"two",
    3:"three",
    4:"four",
    5:"five",
    6:"six",
}


$(()=>{

    // 页面初始化
    function pageInfoInit(params) {

        const drawNums = myReward.getDrawNums()

        // console.log("drawNums",drawNums)
        currentDrawNum.attr("type",currentDrawNumMap[drawNums]) 

        if(drawNums ===0 ) getEggBtn.addClass("disabled")
        else getEggBtn.removeClass("disabled")

    }

    // 奖品列表刷新
    function rewardListRefresh(params) {

        const rewarList = myReward.get()
        const timer ="2018/06/06"
        const name = "2"
        const itemModel = `
            <li class="reward_item">
                <p>${timer}</p>
                <p>恭喜你获得</p>
                <p>${name}</p>
            </li>
        `
        let childs = ""
        let hasCanton = false
        let hasQB = false

        rewardListDom.empty()
        rewarList.forEach(item=>{

            if(item.type===4) hasCanton = true
            if(item.type===2) hasQB = true
            childs+= `
                <li class="reward_item">
                    <p>${item.time}</p>
                    <p>恭喜你获得</p>
                    <p>${item.name}</p>
                </li>
            `
        })

        if(rewarList.length===0){
            rewardListDom.append(`<li class="reward_empty">暂无奖品信息</li>`)
            reward_btn.hide()
        }else{

            if(hasCanton || hasQB){
                reward_btn.show()
                
                // 不同奖品显示不同的按钮
                if(hasCanton && hasQB){
                    reward_btn.attr("type","getQBAndChangeAdress")
                }else if(hasCanton){
                    reward_btn.attr("type","writeAdress")
                }else if(hasQB){
                    reward_btn.attr("type","getQB")
                }
            }
            else{
                reward_btn.hide()
            }

            rewardListDom.append(childs)
        }
        
    }

    /***
     *  初始化工作
     *  1、抽奖次数
     *  2、按钮颜色
     *  3、奖品列表
     */

    const getEggBtn = $(".get_egg_btn") // 点击摇奖按钮
    const currentDrawNum = $(".lucky_num") // 抽奖次数
    const rewardListDom = $(".reward_list") // 奖品列表
    const activity_egg_machine = $(".activity_egg_machine") // 摇奖机
    const rewardModal = $(".reward_modal") // 奖品弹窗
    const egg_item_reward_type = $(".egg_item_reward_type") // 奖品名字
    const egg_item_reward_tip = $(".egg_item_reward_tip") // 奖品信息
    const reward_btn = $(".reward_btn") //  奖品列表的领取奖品
    const reward_modal_reward_btn = $(".reward_modal .get_reward_btn") //  奖品弹窗的按钮

    // 下面的几个单需要根据抽奖的不同显示不同的蛋
    const s_egg_item = $(".s_egg_item") // 小蛋
    const b_ess_item_mid_item = $(".b_egg_item.mid_item") // 蛋中
    const b_ess_item_bottom_item = $(".b_egg_item.bottom_item") // 蛋底
    const eggType = rewardData.egg_reward_map
    

    pageInfoInit()
    rewardListRefresh()

    // 中奖信息轮播
    new Swiper('.swiper-container', { 
        mode : 'vertical',
        speed:800,
        // autoHeight: true, //高度随内容变化
        loop : true,
        autoplay: 3000
    });

    // 关闭中奖信息弹窗
    $(".egg_item_info_close").on("click",(e)=>{
        rewardModal.hide();
        activity_egg_machine.removeClass("drawing")
        rewardModal.removeClass("opening")
    })

    // 点击摇奖
    let DRAWING = false // 当前是否正在摇奖，以免多次点击
    getEggBtn.on("click",()=>{

        const drawNums = myReward.getDrawNums()

        // 次数小于0 不能摇奖
        if(drawNums<=0 || DRAWING) return 

        DRAWING = true

        // 概率抽奖
        const reward = myReward.draw()
        const rewardType = reward.type
        const eggTypee = eggType[rewardType]

        myReward.save(reward) // 保存奖品

        // 设置蛋的颜色 rewardData
        s_egg_item.attr("type",eggTypee)
        b_ess_item_mid_item.attr("type",eggTypee)
        b_ess_item_bottom_item.attr("type",eggTypee)

        // 设置奖品信息
        egg_item_reward_type.text(reward.name)
        egg_item_reward_tip.text(reward.tip)

        if(rewardType===2){
            reward_modal_reward_btn.show()
            reward_modal_reward_btn.attr("type","getQB")
        }else if(rewardType===4){
            reward_modal_reward_btn.show()
            reward_modal_reward_btn.attr("type","writeAdress")
        }else {
            reward_modal_reward_btn.hide()
        }

        // 减少一次抽奖次数
        myReward.setDrawNums(-1)

        // 开始摇奖动画
        activity_egg_machine.addClass("drawing")

        console.log("reward",reward)

        pageInfoInit() // 初始化
        rewardListRefresh() // 奖品刷新

        // 显示奖品信息
        setTimeout(() => {
            rewardModal.show()
            DRAWING = false
            // 开蛋动画
            setTimeout(() => {
                rewardModal.addClass("opening")
            },100);
        }, 2500);
        

    })

    rewardModal.on("touchmove",(e)=>{
        console.log(e)
        e.preventDefault()
        e.stopPropagation()
    })
    // 弹窗的领取按钮
    reward_modal_reward_btn.on("click",()=>{
        const type = reward_modal_reward_btn.attr("type")

        if(type==="getQB"){
            Toast("领取成功")
        }else if(type==="writeAdress"){
            Toast("填写成功")
        }

    })

    // 奖品列表的领取按钮
    reward_btn.on("click",()=>{

        const type = reward_btn.attr("type")

        if(type==="getQB"){
            Toast("领取成功")
        }else if(type==="getQBAndChangeAdress"){
            Toast("填写成功")
        }

    })

    // 购买课程
    $(".buy_lesson_btn").on("click",()=>{
        
        // 增加一次抽奖次数
        const drawNums = myReward.setDrawNums(1)

        if(drawNums>=6) Toast("对不起，已经超过上限，无法获得抽奖机会！")

        else Toast("恭喜您获得抽奖机会！")

        pageInfoInit()

    })

})



// 奖品信息

// const Toast


// console.log("img",$)